gusucode.com > VC 设置开机自动运行-源码程序 > VC 设置开机自动运行-源码程序/code/StartAutoRunDlg.cpp
//Download by http://www.NewXing.com // StartAutoRunDlg.cpp : implementation file // #include "stdafx.h" #include "StartAutoRun.h" #include "StartAutoRunDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CStartAutoRunDlg dialog CStartAutoRunDlg::CStartAutoRunDlg(CWnd* pParent /*=NULL*/) : CDialog(CStartAutoRunDlg::IDD, pParent) { //{{AFX_DATA_INIT(CStartAutoRunDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CStartAutoRunDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CStartAutoRunDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CStartAutoRunDlg, CDialog) //{{AFX_MSG_MAP(CStartAutoRunDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND(ID_MENUCANCEL, OnMenucancel) ON_COMMAND(ID_MENURUN, OnMenurun) ON_WM_CTLCOLOR() ON_WM_RBUTTONUP() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CStartAutoRunDlg message handlers BOOL CStartAutoRunDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here m_Menu.LoadMenu(IDR_MENU1); return TRUE; // return TRUE unless you set the focus to a control } void CStartAutoRunDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CStartAutoRunDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } CDC* pDC = GetDC(); CFont m_Font,m_DateFont; CRect rect,rc; GetClientRect(rect); //获得客户区域 CTime time = CTime::GetCurrentTime(); //获得系统时间 m_Font.CreatePointFont(120,_T("宋体"),pDC); //创建字体 pDC->SelectObject(&m_Font); //选入字体 pDC->SetBkMode(TRANSPARENT); //设置背景透明 rc.CopyRect(rect); //拷贝区域 rc.DeflateRect(0,50,0,0); //缩小区域 pDC->DrawText(time.Format("%Y年%m月"),rc,DT_CENTER); //绘制年月 m_Font.DeleteObject(); //删除字体 m_DateFont.CreatePointFont(800,_T("黑体"),pDC); //创建日期字体 pDC->SelectObject(&m_DateFont); //选入日期字体 rc.CopyRect(rect); //拷贝客户区域 rc.DeflateRect(0,60,0,0); //缩放区域 pDC->DrawText(time.Format("%d"),rc,DT_CENTER); //绘制日期阴影 pDC->SetTextColor(RGB(255,0,0)); //设置文本颜色 rc.CopyRect(rect); //拷贝区域 rc.DeflateRect(0,59,5,0); //缩放区域 pDC->DrawText(time.Format("%d"),rc,DT_CENTER); //绘制日期 m_DateFont.DeleteObject(); //删除日期字体 } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CStartAutoRunDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CStartAutoRunDlg::OnMenucancel() { // TODO: Add your command handler code here HKEY sub; char bufname[200]; ::GetModuleFileName(NULL,bufname,200); //获得路径 CString str; str.Format("%s",bufname); CString skey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; //设置注册表键 ::RegCreateKey(HKEY_LOCAL_MACHINE,skey,&sub); //打开注册表键 ::RegDeleteValue(sub,"StartAutoRun"); //取消开机运行 WritePrivateProfileString("设置","开机运行","0","./setting.ini"); //写入键值 } void CStartAutoRunDlg::OnMenurun() { // TODO: Add your command handler code here HKEY sub; char bufname[200]; ::GetModuleFileName(NULL,bufname,200); //获得路径 CString str; str.Format("%s",bufname); CString skey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; //设置注册表键 ::RegCreateKey(HKEY_LOCAL_MACHINE,skey,&sub); ::RegSetValueEx(sub,"StartAutoRun",NULL,REG_SZ ,(const BYTE*)str.GetBuffer(0),str.GetLength()); //设置开机运行 WritePrivateProfileString("设置","开机运行","1","./setting.ini"); //写入键值 } HBRUSH CStartAutoRunDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here CBitmap m_BKGround; m_BKGround.LoadBitmap(IDB_BITMAP1); if (nCtlColor==CTLCOLOR_DLG) { CBrush m_Brush(&m_BKGround); //定义一个位图画刷 CRect rect; GetClientRect(rect); //获得客户区域 pDC->SelectObject(&m_Brush); //选中画刷 pDC->FillRect(rect,&m_Brush); //填充客户区域 return m_Brush; } else hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); return hbr; } void CStartAutoRunDlg::OnRButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMenu* pPopup = m_Menu.GetSubMenu(0); CRect rc; ClientToScreen(&point); rc.top = point.x; rc.left = point.y; char keys[MAX_PATH] = {0}; //定义一个字符数组 GetPrivateProfileString("设置","开机运行","", keys,MAX_PATH,"./setting.ini"); //读取透明度 CString str = keys; if(str == "1") { m_Menu.EnableMenuItem(ID_MENURUN,MF_GRAYED); m_Menu.EnableMenuItem(ID_MENUCANCEL,MF_ENABLED); } else { m_Menu.EnableMenuItem(ID_MENURUN,MF_ENABLED); m_Menu.EnableMenuItem(ID_MENUCANCEL,MF_GRAYED); } pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_VERTICAL, rc.top,rc.left,this,&rc); CDialog::OnRButtonUp(nFlags, point); }